const {
  $util,
} = getApp();
Page({
  data: {
    page: 1,
    list: [],
    loaded: false,
    selectedUnit: {},
    uploading: false,
    isShow: false,
  },
  onLoad() {
    this.loadData();
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    })
  },
  loadData() {
    $util.request({
      url: 'entry/wxapp/edu.change.getdetail',
      data: {},
      success: (res) => {
        this.setData({
          userInfo: res.info,
          organ: res.organ,
          loaded: true,
        });
      },
    });
  },
  pick(e) {
    this.setData({
      isShow: true,
    })
  },
  getOrgan(e) {
    this.setData({
      selectedUnit: e.detail,
    })
  },
  setRenumber(e) {
    this.setData({
      renumber: e.detail.value,
    });
  },
  submit() {
    const D = this.data;
    if (!D.selectedUnit.id) {
      wx.showToast({
        title: '请输入转入单位',
        icon: 'none',
      });
      return;
    }
    if (!D.renumber) {
      wx.showToast({
        title: '请输入介绍信编号',
        icon: 'none',
      });
      return;
    }
    let regExp = /^\d+$/g;
    if (!regExp.test(D.renumber)){
      wx.showToast({
        title: '请输入正确的介绍信编号',
        icon: 'none',
      });
      return;
    }
    this.setData({
      uploading: true,
    });
    $util.request({
      url: 'entry/wxapp/edu.change.upload',
      data: {
        intotheunit: D.selectedUnit.id,
        renumber: D.renumber,
      },
      success: () => {
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 1e3,
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 1e3);
      },
      complete: () => {
        this.setData({
          uploading: false,
        });
      },
    });
  },
});
